function adlogBooking(data) {	
	var date = data.date;
	var size = $('#people').val();
	var time = data.time;
	var promotion = $('#promotionId').val();
	var confirmation = data.confirmationNumber;
	var reservation = data.reservationId;
	var restaurant = $('#ga_restaurantRNumber').val();
	var script = document.createElement('script');
	script.type = 'text/javascript';	
	var url = 'http://js.dmtry.com/channel.js#gid:2758;advid:766;pid:182498;'+ 'bid:' + reservation + ';con:' + confirmation + ';rst:' + restaurant + ';cvs:' + size + ';dte:' + date + ';tme:' + time + ';btp:' + promotion + '';
	script.src = url;
	/*
	var getData = {
			params: reservation + confirmation + restaurant + size + date + time + promotion
	};
		
	var request = $.ajax({
		  url: url,
		  data: getData,
		  type: "POST",
		  dataType: "html",
		  async: false,
		  cache: true,
		  //crossDomain: true
		});
		*/
//	document.write('<scr' + 'ipt type="tex' + 't/jav' + 'ascr' + 'ipt" src="' + url + '"><\scri'+'pt>');
	//$('body').append('<div id="dmtryWraper"></div>');
	//$('#dmtryWraper').append('<scr' + 'ipt type="tex' + 't/jav' + 'ascr' + 'ipt" src="' + url + '"><\scri'+'pt>');
	var head = document.getElementsByTagName("head")[0];
	head.appendChild(script);
	
	//eval(data.counters);
	
	$(data.counters).appendTo('body');
	
	
}

function resize(el,val){
	if ($('#ruler').get(0)) {
		$('#ruler').css('fontFamily',el.css('fontFamily') );
		$('#ruler').css('fontSize',el.css('fontSize'));
		$('#ruler').text(val);
		//console.log(el.attr('id') + ' - ' + $('#ruler').width());
		el.val(val);
	        if (el.is($('#location'))) {
	          // for the location field the size if off by this factor
	          // this is a workaround for that        	
//	          el.width(($('#ruler').width() + 2 - 175).toString());
	          //el.width(($('#ruler').width()).toString());
	        } else if (el.is($('#date'))) {
	            //slims the size of the date field based on the percentage of width.
	          el.width(($('#ruler').width() - (($('#ruler').width()/100)*6)));
	        } else {
	          el.width(($('#ruler').width() + 2));
	        }
	}
}




String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

//update(0);

function get_address(name) {

    return restaurants[name];

}
/*
$.datepicker.setDefaults({
    'dateFormat': 'DD d, MM'
});

$('#date').datepicker({
	onSelect: function(date){

		resize($('#date'),date);

	},
	minDate: new Date()
	});
*/
//$('input#location').autoGrowInput();

var t=setTimeout(function(){

resize($('#date'),$('#date').val());
resize($('#location'),$('#location').val());

resize($('#time'),$('#time').val());

$('#px_form').customFadeIn();
},400);


$('#px_form').find('input').click(function(){ 

	$('#booking_ui').removeClass('inactive');

});



function preset_time(){

$('#time_select > li').removeClass('selected');
$('#time_select > li:contains('+$('#time').val()+')').addClass('selected');

}

function booking_ui(){

$('#no_ip').customFadeOut(speed,function(){

$('#booking_ui').customFadeIn(speed);

});

}

function ui_input(b){

var disabled = ( b ) ? false : true;

$('#booking_ui').find('input').prop('disabled', disabled);

}

function update()
{

	//update booking_ui components
	if(arguments.length == 1)
	{
		var tmp = getRestaurant(arguments[0]);
		resize($('#location'),tmp.RestaurantName);
		$('.address').text(tmp.Address);
	}
	
	preset_time();
	
	//load restaurant info for single suggestion
	
	//$('#suggested_single').find('h3').text(tmp.Address);
	//$('#suggested_single').show();
	//$('.reserved').show();

}
$(document).ready(function() {
	$('#people').focus(function() {
		if (!$(this).hasClass('alreadyFocused')) {
			if (!$.browser.webkit) {
				$(this).addClass('alreadyFocused');
			    $(this).select();
			}
		}
	}).blur(function(){
		$(this).removeClass('alreadyFocused');
	});
	
	if ($.browser.webkit) {
		$('#people').click(function() {
			if (!$(this).hasClass('alreadyFocused')) {
				$(this).addClass('alreadyFocused');
			    $(this).select();
			}				    
		});
	}
	
	$('#promotionId').live('change', function() {
		$('#promotionChanged').val(1);
		$('#promotionIdWrap .msg').hide();
		if ($('#availability').get(0)) {
			reloadAvailibility();
    	}
	});
	
	$('#people').change(function(){
    	if ($('#availability').get(0)) {    		
    		if ($('#visit-a-restaurant_page').length == 0 && $('#offers_page').length == 0) {
    			reloadAvailibility();
    		}
	    }
    });
	
});

function reloadAvailibility() {
        
    if ($('#bookingRestaurantTitle').length > 0) {
        var branchAddress = $('#bookingRestaurantTitle').html();
        //var branchAddress = $('#locationOriginal').val();
    } else {
        var branchAddress = $('#location').val();
    }
    
    var date = $('#date').val();
    var time = $('#time').val();
    var people = $('#people').val();
    var promotionId = $('#promotionId').val();
    var url = '/livebooking/get-availability/?' +
    	'address='    
    var rLiveBookingsId = 0;
    if ($('#rLiveBookingsId').get(0)) {
    	rLiveBookingsId = $('#rLiveBookingsId').val();
    }
    $('.error-booking').remove();
    $('.form_error').remove();
    $('#availability fieldset').html('<div class="wait-loading loader_spinner" style="margin-top:20px">&nbsp;</div>');
    $.get("/livebooking/get-availability/", { 
    		rLiveBookingsId: rLiveBookingsId, 
    		branchAddress: branchAddress, 
    		size: people,
    		date: date,
    		promotionId: promotionId,
    		time: time,
 		    promotionChanged : $('#promotionChanged').val(),
		    pageCompany : $('#pageCompany').val()           		
    		}, function(data) {
    			$('.error-booking').remove();
        		$('.form_error').remove();
    			$('#availability').html(data);   	    			
    		} );    
    //$('.wait-loading').remove();
}

/* use labels as default value on registration form inputs */
/*
$('#registration_form').find('input:text').prev().css({position:'absolute',left:'-10000px'});

$('#registration_form').find('input').not('#register').css('color','#ccc');

$('#registration_form').find('input').focus(function(){ 

if( $(this).val() == $(this).prev().html() ){ $(this).val(''); $(this).css('color','#000'); }

});

$('#registration_form').find('input').blur(function(){

if( $(this).val() == '' ){ $(this).val( $(this).prev().html() ); $(this).css('color','#ccc'); }

});*/
